package com.qulai.web.controller.basic;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.qulai.common.utils.http.HttpUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.qulai.common.annotation.Log;
import com.qulai.common.core.controller.BaseController;
import com.qulai.common.core.domain.AjaxResult;
import com.qulai.common.enums.BusinessType;
import com.qulai.amazon_us.domain.basic.Brand;
import com.qulai.amazon_us.service.basic.IBrandService;
import com.qulai.common.utils.poi.ExcelUtil;
import com.qulai.common.core.page.TableDataInfo;

import static com.qulai.common.utils.http.HttpUtils.flush_basic_brand_sensitive;

/**
 * 品牌库Controller
 *
 * @author hunan
 * @date 2025-01-03
 */
@RestController
@RequestMapping("/basic/brand")
public class BrandController extends BaseController {


    private static final Logger log = LoggerFactory.getLogger(BrandController.class);
    @Autowired
    private IBrandService brandService;

    /**
     * 查询品牌库列表
     */
    @PreAuthorize("@ss.hasPermi('basic:brand:list')")
    @GetMapping("/list")
    public TableDataInfo list(Brand brand) {
        startPage();
        List<Brand> list = brandService.selectBrandList(brand);
        return getDataTable(list);
    }

    /**
     * 导出品牌库列表
     */
    @PreAuthorize("@ss.hasPermi('basic:brand:export')")
    @Log(title = "品牌库", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Brand brand) {
        List<Brand> list = brandService.selectBrandList(brand);
        ExcelUtil<Brand> util = new ExcelUtil<Brand>(Brand.class);
        util.exportExcel(response, list, "品牌库数据");
    }

    /**
     * 获取品牌库详细信息
     */
    @PreAuthorize("@ss.hasPermi('basic:brand:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(brandService.selectBrandById(id));
    }

    /**
     * 新增品牌库
     */
    @PreAuthorize("@ss.hasPermi('basic:brand:add')")
    @Log(title = "品牌库", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Brand brand) {
        brand.setUpdateBy(getUsername());
        try {
            int i = brandService.insertBrand(brand);
            Map<String, Object> params = new HashMap<>();
            params.put("version", "1.0");
            HttpUtils.sendPostToPython(flush_basic_brand_sensitive, params);
        } catch (Exception e) {
            if (e.getMessage().contains("Duplicate entry")) {
                throw new RuntimeException("该品牌/敏感词已经添加过");
            } else {
                log.error(e.getMessage());
                throw new RuntimeException("更新品牌/敏感词json失败");
            }
        }
        return toAjax(true);
    }

    /**
     * 修改品牌库
     */
    @PreAuthorize("@ss.hasPermi('basic:brand:edit')")
    @Log(title = "品牌库", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Brand brand) {
        brand.setUpdateBy(getUsername());
        int i = brandService.updateBrand(brand);
        Map<String, Object> params = new HashMap<>();
        params.put("version", "1.0");
        try {
            HttpUtils.sendPostToPython(flush_basic_brand_sensitive, params);
        } catch (Exception e) {
            log.error(e.getMessage());
            throw new RuntimeException("更新品牌/敏感词json失败");
        }
        return toAjax(i);
    }

    /**
     * 删除品牌库
     */
    @PreAuthorize("@ss.hasPermi('basic:brand:remove')")
    @Log(title = "品牌库", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        int i = brandService.deleteBrandByIds(ids);
        try {
            Map<String, Object> params = new HashMap<>();
            params.put("version", "1.0");
            HttpUtils.sendPostToPython(flush_basic_brand_sensitive, params);
        } catch (Exception e) {
            log.error(e.getMessage());
            throw new RuntimeException("更新品牌/敏感词json失败");
        }
        return toAjax(i);
    }
}
